home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4236 < prev    next >
Encoding:
Text File  |  1996-08-06  |  6.5 KB  |  259 lines

  1. Newsgroups: comp.lang.c++
  2. Path: tday.slip.netcom.com!user
  3. From: tday@netcom.com (Tony Day)
  4. Subject: Help with Bug Pleeeeease!
  5. Message-ID: <tday-2801961838030001@tday.slip.netcom.com>
  6. Sender: netnews@mork.netcom.com
  7. Nntp-Posting-Host: tday.slip.netcom.com
  8. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  9. Date: Mon, 29 Jan 1996 01:38:03 GMT
  10.  
  11.  
  12. I hope some C++ wizard out there can help me debug this apparantly simple
  13. program.  I am teaching myself C++ with Prata╣s │C++ primer +▓ and am
  14. completely stuck with one of the problems at the end of CH11.
  15.  
  16. The aim of the excercise is to overload the +operator for a String Class
  17. (which consists of a char * and int holding the string length). I have
  18. written 3 overloaded + operators, one adds a String to a String (this
  19. seems to work), the other two add a C string to a String.  
  20.  
  21. The problem is with the latter two.  When the assignment operator (not
  22. written by me) is invoked after one of these + operators, the whole system
  23. crashes (I am using Symantec Think C++ 7.04 on a Mac quadra) even if the
  24. assignment operator invokes different Strings to the + operators. This is
  25. what happens when the main (below) is run.  If the assignment call is
  26. before the +operator call, no problem. 
  27.  
  28. I have put îcout╣s everywhere to try and track what is going on.  The
  29. program crashes at │str = new char[len + 1]▓ in the = operator.  The
  30. problem appears to be caused by │String temp = *this+tempo;▓ in the +
  31. operators.  
  32.  
  33. Another strange thing (that happens in all programs) is that temporary
  34. class variables which are created in a function and then returned by value
  35. are not destructed until the program ends.  Is this what is meant to
  36. happen?
  37.  
  38. Many thanks to whichever kind souls can solve this and give me my sanity back.
  39.  
  40. Tony
  41.  
  42. Source code follows:
  43.  
  44.  
  45.  
  46. MAIN FILE
  47.  
  48. //pe11_11.1.cpp
  49. #include <iostream.h>
  50. #include "oztrng2.h"
  51.  
  52. int main(void)
  53. {
  54.    String s1(" and I am a C++ student.");
  55.    String s2 = "Please enter your name: ";
  56.    String s3;
  57.    cout << s2;
  58.    cin >> s3;
  59.    
  60.    String temp = s3 + "my name is ";
  61.    s1=s2;
  62.       
  63.    cout << "Bye\n";
  64.    return 0;
  65. }
  66.  
  67. HEADER FILES
  68.  
  69. // booly.h -- Boolean definitions
  70. // eventually to be replaced by new C++ bool type
  71. #ifndef _BOOLY_H_
  72. #define _BOOLY_H_
  73. enum Bool {False, True}; // False = 0, True = 1
  74. #endif
  75.  
  76.  
  77. // strng2.h -- String class definition
  78. #ifndef _STRNG2_H_
  79. #define _STRNG2_H_
  80. #include <iostream.h>
  81. #include "booly.h"  // our definitions of Bool, False, and True
  82. class String
  83. {
  84. private:
  85.    char * str;                   // pointer to string
  86.    int len;                      // length of string
  87. public:
  88.    String(const char * s);       // constructor
  89.    String();                     // default constructor
  90.    String(const String & st);
  91.    ~String();                    // destructor
  92.    int length () { return len; }
  93.  
  94. // overloaded operators
  95.    String & operator=(const String & st); // Assignment operator
  96.    String & operator=(const char * s); // Assignment operator #2
  97.    String operator+(const String & st) const;
  98.    String operator+(const char * s) const;
  99.    
  100. // friend functions
  101.    
  102.    friend String operator+(const char * s, const String & st);
  103.    friend Bool operator>(const String &st1, const String &st2);
  104.    friend Bool operator<(const String &st, const String &st2);
  105.    friend Bool operator==(const String &st, const String &st2);
  106.    friend ostream & operator<<(ostream & os, const String & st);
  107.    friend istream & operator>>(istream & is, String & st);
  108. };
  109. #endif
  110.  
  111. CLASS DEFINITION FILE
  112.  
  113. // strng2.cpp -- String class methods
  114. #include <iostream.h>
  115. #include <string.h>
  116. #include <ctype.h>
  117. #include "oztrng2.h"
  118.  
  119.  
  120. // class methods
  121.  
  122. String::String(const char * s)   // make String from C string
  123. {
  124.    len = strlen(s);
  125.    str = new char[len + 1];        // allot storage
  126.    strcpy(str, s);                 // initialize pointer
  127.    cout << "created c++ String" <<str;
  128. }
  129.  
  130. String::String()           // default constructor
  131. {
  132.    len = 0;
  133.    str = new char[1];
  134.    str[0] = '\0';               // default string
  135.    cout << "create default String";
  136. }
  137.  
  138. String::String(const String & st)   // copy constructor
  139. {
  140.    len = st.len;
  141.    str = new char[len + 1];
  142.    strcpy(str, st.str);
  143.    cout << "create String string (copy)"<<str << "\n";
  144. }
  145.  
  146. String::~String()          // destructor
  147. {
  148.    cout << "destroy "<< str<< "\n";
  149.    delete [] str;               // required
  150.    
  151. }
  152.  
  153.    // assign a String to a String
  154. String & String::operator=(const String & st)
  155. {  cout << "top of assign";
  156.    if (this == &st)
  157.       return *this;
  158.    delete [] str;
  159.    len = st.len;
  160.    str = new char[len + 1];
  161.    strcpy(str, st.str);
  162.    cout << "assign STRING\n";
  163.    return *this;
  164. }
  165.  
  166.    // assign a C string to a String
  167. String & String::operator=(const char * s)
  168. {
  169.    delete [] str;
  170.    len = strlen(s);
  171.    str = new char[len + 1];
  172.    strcpy(str, s);
  173.    cout << "assign string\n";
  174.    return *this;
  175. }
  176.  
  177. String String::operator+(const String & st) const
  178. {
  179.    String temp1;
  180.    delete [] temp1.str;
  181.    temp1.len = st.len+len;
  182.    temp1.str = new char[temp1.len+1];
  183.    strcpy(temp1.str,str);
  184.    for (int i=len; i< temp1.len; i++)
  185.       temp1.str[i]=st.str[i-len];
  186.    temp1.str[temp1.len+1]='\0';
  187.    cout << temp1.len << " length1st\n";
  188.    return temp1;
  189. }
  190.  
  191. String String::operator+(const char * s) const
  192. {
  193.    String tempo;
  194.    delete [] tempo.str;
  195.    tempo.len=strlen(s);
  196.    tempo.str = new char[tempo.len+1];
  197.    strcpy(tempo.str,s);
  198.    String temp = *this+tempo;
  199.    cout << temp.len << " length2nd\n";
  200.    return temp;
  201. }
  202.  
  203. String operator+(const char * s, const String & st)
  204. {
  205.    String tempo;
  206.    delete [] tempo.str;
  207.    tempo.len=strlen(s);
  208.    tempo.str = new char[tempo.len+1];
  209.    strcpy(tempo.str,s);
  210.    String temp = tempo+st;
  211.    cout << temp.len << " length3rd\n";
  212.    return temp;
  213. }  
  214.    
  215.    // true if st1 follows st2 in collating sequence
  216. Bool operator>(const String &st1, const String &st2)
  217. {
  218.    if (strcmp(st1.str, st2.str) > 0)
  219.       return True;
  220.    else
  221.       return False;
  222. }
  223.  
  224.  
  225.    // true if st1 precedes st2 in collating sequence
  226. Bool operator<(const String &st1, const String &st2)
  227. {
  228.    if (strcmp(st1.str, st2.str) < 0)
  229.       return True;
  230.    else
  231.       return False;
  232. }
  233.  
  234.    // true if st1 is the same as st2
  235. Bool operator==(const String &st1, const String &st2)
  236. {
  237.    if (strcmp(st1.str, st2.str) == 0)
  238.       return True;
  239.    else
  240.       return False;
  241. }
  242.  
  243.    // display string
  244. ostream & operator<<(ostream & os, const String & st)
  245. {
  246.    os << st.str;
  247.    return os;
  248. }
  249.  
  250.    // quick and dirty String input
  251. istream & operator>>(istream & is, String & st)
  252. {
  253.    char temp[80];
  254.    is.getline(temp, 80);
  255.    if (is)
  256.       st = temp;
  257.    return is;
  258. }
  259.